package com.bike.controller.api;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.bike.controller.BaseController;
import com.bike.entity.HhBusinessUser;
import com.bike.entity.HhOrderFood;
import com.bike.service.impl.HhBusinessUserServiceImpl;
import com.bike.service.impl.HhOrderServiceImpl;
import com.bike.util.PageData;
import com.bike.util.TimeManager;
import com.bike.vo.PcJsonView;
import com.fasterxml.jackson.annotation.JsonView;

 
@Controller
@RequestMapping(value="/apiOrder")
public class HhApiOrderController extends BaseController {
	
		@Autowired
		private HhOrderServiceImpl  orderServiceImpl;
		@Autowired
		private HhBusinessUserServiceImpl  businessUserServiceImpl;
	
		/**
		 * 列表查询(或根据条件)
		 * param   page
		 */
		@RequestMapping("list")
		@ResponseBody
		@JsonView(PcJsonView.class)
		public PageData list(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			Long userId=pd.getLong("userId");
			HhBusinessUser businessUser=(HhBusinessUser) request.getAttribute("businessUser");
			pd.put("businessId", businessUser.getBusinessId());
			pd.put("userId", null);
			pd.put("payStatus", 1);

			String format="yyyy-MM-dd HH:mm:ss";
			pd.put("startTime", TimeManager.stringTotime(pd.getString("startTime"), format));
			pd.put("endTime", TimeManager.stringTotime(pd.getString("endTime"), format));
			
			//0全部1待配送2已配送3成功 必传   0全部
			if(pd.getInteger("orderStatus")==0){
				pd.put("orderStatus", null);
			}
			
			PageData resultPd=getSuccessPd();
			if(pd.getInteger("orderStatus")!=0){
				resultPd.put("rows",orderServiceImpl.selectByPd(pd) );
				resultPd.put("total",0 );
			}else{
				PageData orderPd=orderServiceImpl.selectHhOrderList(pd);
				resultPd.put("rows",orderPd.get("rows") );
				resultPd.put("total",orderPd.get("total") );
			}
			return resultPd;
		}
		/**
		 * 更改订单配送状态
		 * param   page
		 */
		@RequestMapping("updateSend")
		@ResponseBody
		public PageData updateSend(HttpServletRequest request,Model model){
			PageData pd=PageData.getPageDataByPage(request);
			Long userId=pd.getLong("userId");//用户id
			Long orderId=pd.getLong("orderId");//订单id
			Long sendUserId=pd.getLong("sendUserId");//配送人订单id
			Long businessId=pd.getLong("businessId");//商户id
			
			PageData resultPd=getSuccessPd();
			orderServiceImpl.updateOrderSendStatus(userId,orderId,sendUserId,businessId);
			return resultPd;
		}
		/**
		 * 商品明细
		 * param   page
		 */
		@RequestMapping("orderItem")
		@ResponseBody
		public PageData orderItem(HttpServletRequest request,Model model){
			PageData pd=PageData.getToPageData(request);
			Long orderId=pd.getLong("orderId");
			
			PageData resultPd=getSuccessPd();
			List<HhOrderFood> hhOrderFood=orderServiceImpl.selectOrderItemByOrderId(pd);
			resultPd.put("rows",hhOrderFood );
			if(pd.getInteger("businessId")<=0){
				 return getErrorPd("请求参数错误");
			}
			return resultPd;
		}
}
